﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

#if UNITY_EDITOR
using UnityEditor;
using UnityEditor.Animations;
using System.Linq;
#endif
[RequireComponent(typeof(Animator))]
public class CheckExportAnim : MonoBehaviour
{
    public GameObject cube;

}

#if UNITY_EDITOR
[CustomEditor(typeof(CheckExportAnim))]
public class CheckExportAnimEditor : Editor
{
    private AnimationClip[] m_Clips = null;
    private CheckExportAnim m_Script = null;
    void OnEnable()
    {
        m_Script = (target as CheckExportAnim);
        Animator animator = m_Script.gameObject.GetComponent<Animator>();
        AnimatorController controller = (AnimatorController)animator.runtimeAnimatorController;
        m_Clips = controller.animationClips;
    }

    private int m_SelectIndex = 0;
    private float m_SliderValue = 0;
    public override void OnInspectorGUI()
    {
        base.OnInspectorGUI();

        EditorGUI.BeginChangeCheck();
        m_SelectIndex = EditorGUILayout.Popup("动画", m_SelectIndex, m_Clips.Select(pkg => pkg.name).ToArray());
        m_SliderValue = EditorGUILayout.Slider("进度", m_SliderValue, 0f, 1f);
        if (EditorGUI.EndChangeCheck())
        {
            AnimationClip clip = m_Clips[m_SelectIndex];
            float time = clip.length * m_SliderValue;
            clip.SampleAnimation(m_Script.gameObject, time);

            //验证导出的动画
            if (m_Script.cube != null)
            {
                var test = m_Script.cube.GetComponent<ParseAnim>();
                if (test != null)
                {
                    test.Init();
                    if (test.animTransformData != null)
                    {
                        float testTime = test.animTransformData.length * m_SliderValue;
                        test.SampleAnimation(testTime);
                    }
                }
            }
        }
    }
}



#endif